package wangxiaotao.tool;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeCloseRequest;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import lombok.extern.slf4j.Slf4j;
import wangxiaotao.config.AliPayProperties;
import wangxiaotao.entity.alipay.AliPayRequest;
import wangxiaotao.entity.alipay.PayOrderRequest;
import wangxiaotao.utils.SpringUtils;
@Slf4j
public class AliPayTool {
    private static final AliPayProperties aliPayProperties = (AliPayProperties) SpringUtils.getBean("aliPayProperties");

    /**
     * 支付
     * @param alipayBean
     * @return
     * @throws AlipayApiException
     */
    public static String pay(AliPayRequest alipayBean) throws AlipayApiException {
        AlipayClient alipayClient = getAlipayClient();
        AlipayTradePrecreateRequest alipayRequest = new AlipayTradePrecreateRequest (); //创建API对应的request类
        alipayRequest.setNotifyUrl(aliPayProperties.getNotifyUrl());
        alipayRequest.setBizContent(JSON.toJSONString(alipayBean));
        return alipayClient.execute(alipayRequest).getQrCode();
    }

    /**
     * 查询订单状态
     * @param payOrderRequest
     * @return
     * @throws AlipayApiException
     */
    public static String query(PayOrderRequest payOrderRequest) throws AlipayApiException {
        AlipayClient alipayClient = getAlipayClient();
        AlipayTradeQueryRequest alipayRequest = new AlipayTradeQueryRequest();
        alipayRequest.setBizContent(JSON.toJSONString(payOrderRequest));
        return alipayClient.execute(alipayRequest).getBody();
    }

    /**
     * 关闭订单
     * @param payOrderRequest
     * @throws AlipayApiException
     */
    public static String close(PayOrderRequest payOrderRequest) throws AlipayApiException {
        AlipayClient alipayClient = getAlipayClient();
        AlipayTradeCloseRequest request = new AlipayTradeCloseRequest();
        request.setBizContent(JSON.toJSONString(payOrderRequest));
        return alipayClient.execute(request).getBody();
    }

    /**
     * 取消订单
     * @param payOrderRequest
     * @throws AlipayApiException
     */
    public static void cancel(PayOrderRequest payOrderRequest) throws AlipayApiException {
//        AlipayClient alipayClient = getAlipayClient();
//        AlipayTradeQueryRequest alipayRequest = new AlipayTradeQueryRequest();
//        alipayRequest.setBizContent(JSON.toJSONString(payOrderRequest));
    }

    public static AlipayClient getAlipayClient() throws AlipayApiException {
        //1、获得初始化的AlipayClient
        return new DefaultAlipayClient(
                aliPayProperties.getGatewayUrl(),//支付宝网关
                aliPayProperties.getAppId(),//appid
                aliPayProperties.getPrivateKey(),//商户私钥
                "json",
                aliPayProperties.getCharset(),//字符编码
                aliPayProperties.getAlipayPublicKey(),//支付宝公钥
                aliPayProperties.getSignType()//加密方式
        );
    }
}
